package com.iu.control;

import com.google.gson.Gson;
import com.iu.dao.UserDao;
import com.iu.uitl.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet( "/login")
public class LoginServlet extends HttpServlet {
    UserDao userDao;//注意要先在代码里赋值再使用
    Result result;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String tel=request.getParameter("phone");
        String pass=request.getParameter("password");
        userDao=new UserDao();
        result=new Result();
        result.isSuccess=userDao.userLogin(tel,pass);
        result.msg=result.isSuccess?"登录成功":"登录失败";
        Gson gson=new Gson();
        String res=gson.toJson(result);
        response.getWriter().append(res);
    }
}
